﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace JingQiao.Ads.Infrastructure.Data.LinqToSql.Repository
{
    using Domain.Model;
    using Domain.Model.DomainObject;
    using Domain.Model.Repository;

    public abstract class BaseRepository<TEntity> : IRepository<TEntity>
    {
        protected readonly AdsContext _context;

        protected BaseRepository(IContext context)
        {
            Check.Argument.IsNotNull(context, "context");
            this._context = context as AdsContext;
        }

        public virtual void Add(TEntity entity)
        {
            Check.Argument.IsNotNull(entity, "entity");
            this._context.Insert(entity);
        }

        public virtual void Remove(TEntity entity)
        {
            Check.Argument.IsNotNull(entity, "entity");
            this._context.Delete(entity);
        }

        protected static PagedResult<T> BuildPagedResult<T>(IEnumerable entities, int total)
        {
            return new PagedResult<T>(entities.Cast<T>(), total);
        }

        public AdsContext ContextContainer
        {
            get { return this._context; }
        }
    }
}
